package com.javaGame.util;

import com.javaGame.frame.MainFrame;
import com.javaGame.role.Girl;
import com.javaGame.role.GirlBullet;
import com.javaGame.role.RhombicBullet;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * 键盘事件处理
 */
public class KeyPress implements KeyListener {
    /**是否按下左键*/
    public static boolean isLeft;
    /**是否按下右键*/
    public static  boolean isRight;
    /**是否按下上键*/
    public static  boolean isUp;
    /**是否按下下键*/
    public static  boolean isDown;
    /**是否按下开火键*/
    public static boolean isFire;
    @Override
    public void keyTyped(KeyEvent e) { }

    //键盘按下触发
    @Override
    public void keyPressed(KeyEvent e) {
        //得到键盘事件的键码
        int code = e.getKeyCode();
        if(code == KeyEvent.VK_UP){
            isUp = true;
        }
        else if(code == KeyEvent.VK_DOWN){
            isDown = true;
        }else if(code == KeyEvent.VK_LEFT){
            isLeft = true;
        }
        else if(code == KeyEvent.VK_RIGHT){
            isRight = true;
        }
        else if(code == KeyEvent.VK_A){
            isFire = true;
        }

    }

    //键盘弹起触发
    @Override
    public void keyReleased(KeyEvent e) {
        //得到键盘事件的键码
        int code = e.getKeyCode();
        if(code == KeyEvent.VK_UP){
            isUp = false;
        }
        else if(code == KeyEvent.VK_DOWN){
            isDown = false;
        }else if(code == KeyEvent.VK_LEFT){
            isLeft = false;
        }
        else if(code == KeyEvent.VK_RIGHT){
            isRight = false;
        }
        else if(code == KeyEvent.VK_A){
            isFire = false;

            if(MainFrame.girl.getStatus() == Girl.FORWARD ||
                MainFrame.girl.getStatus() == Girl.BACK) {//前进后退状态可以发子弹

                MainFrame.roleList.add(new GirlBullet());

                if(MainFrame.girl.getMink() != null){//判断是否有宠物
                    MainFrame.roleList.add(new RhombicBullet(RhombicBullet.RIGHTUP));
                    MainFrame.roleList.add(new RhombicBullet(RhombicBullet.RIGHTDOWN));

                    MainFrame.girl.getMink().fireInit();
                }
            }
        }
        else if(code == KeyEvent.VK_S){//发大招
            if(MainFrame.girl.getStatus() == Girl.FORWARD ||
                MainFrame.girl.getStatus() == Girl.BACK){
                MainFrame.girl.eddyInit();
            }
        }
    }
}
